<?php defined('_JEXEC') or die('Restricted access'); ?>
<script language="javascript" type="text/javascript">

	function doSubmit(form, task) {
		if (form) {
			form.task.value = task;
			form.submit();
		}
	}

	function checkScoreValue() {
		var men = !(document.getElementById('scores[player1][1][score]').readOnly);
		var name = men ? 'scores[player1][' : 'scores[player2][';
		for (i = 1; i <= 18; i++) {
			score = document.getElementById(name + i + '][score]').value;
			if (isNaN(score)) {
				alert('Score is not valid. Reinput please!');
				document.getElementById(name + i + '][score]').focus();
				document.getElementById(name + i + '][score]').select();
				return false;
			}
		}
		return true;
	}

	function scorecardCheckAndSubmit() {
		var form = document.scorecardform;
		if (document.getElementById('courseid').selectedIndex < 0) {
			alert("You must choose a golf course first");
		} else if (form.playdate.value == "") {
			alert( "Please enter the play date" );
		} else if (checkScoreValue()) {
			doSubmit(form, 'saveScorecard');
		}
	}

	function onChangeGender() {
		var gender = document.getElementById('usergender').selectedIndex;
		var enabled = 1;
		var disabled = 2;
		if (gender == 1) {
			enabled = 2;
			disabled = 1;
		}
		for (i = 1; i <= 18; i++) {
			document.getElementById('scores[player' + disabled + '][' + i + '][score]').readOnly = true;
			document.getElementById('scores[player' + disabled + '][' + i + '][score]').parentNode.className = 'inputcell_readonly';
			document.getElementById('scores[player' + disabled + '][' + i + '][score]').tabIndex = null;


			document.getElementById('scores[player' + enabled + '][' + i + '][score]').readOnly = false;
			document.getElementById('scores[player' + enabled + '][' + i + '][score]').parentNode.className = 'inputcell';
			document.getElementById('scores[player' + enabled + '][' + i + '][score]').tabIndex = i;
		}
	}
</script>

<table class="contentpaneopen">
	<tbody>
		<tr>
			<td class="contentheading"><?php echo JText::_('My Golf Scorecard'); ?></td>
		</tr>
	</tbody>
</table>

<form action="" method="POST" name="scorecardform">
	<div class="scoresearch_container" style="height:220px">
		<div class="scoresearch">
			<div class="scorecard_box" style="height:180px">
				<h3>Scorecard Search</h3>
				<table cellpading="0" cellspacing="0">
					<tbody>
						<tr>
							<td style="width:130px"><?php echo JText::_('Select Country:'); ?></td>
							<td>
								<?php if($this->country_select): ?>
									<?php echo $this->country_select; ?>
								<?php endif; ?>
							</td>
						</tr>

						<tr>
							<td><?php echo JText::_('Select State:'); ?></td>
							<td>
								<?php if($this->state_select): ?>
									<?php echo $this->state_select; ?>
								<?php endif; ?>
							</td>
						</tr>

						<tr>
							<td><?php echo JText::_('Select Golf Course:'); ?></td>
							<td>
								<?php if($this->course_select): ?>
									<?php echo $this->course_select; ?>
								<?php endif; ?>
							</td>
						</tr>
						<tr>
							<td></td>
							<td>
								<input type="button" class="button" value="Clear" onclick="doSubmit(this.form, 'resetForm')"/>
								<input type="button" class="button" value="Search" onclick="doSubmit(this.form, 'scorecardSearch')"/>
							</td>
						</tr>
					</tbody>
				</table>
			</div>
		</div>
	</div>

	<div class="scorecard_box" style="height: 575px">
		<h3>Scorecard</h3>
		<div class="left">
		<table class="card" cellspacing="0">
			<tbody>
				<tr>
					<td></td>
					<td colspan="4">MEN</td>
					<td colspan="5">WOMEN</td>
					<td colspan="2">PLAYER</td>
				</tr>
				<tr>
					<td>H<br>O<br>L<br>E</td>
					<td>M<br>T<br>R<br>S</td>
					<td>P<br>A<br>R</td>
					<td colspan="2">INDEX</td>
					<td>M<br>T<br>R<br>S</td>
					<td>P<br>A<br>R</td>
					<td colspan="3">INDEX</td>
					<td>M</td>
					<td>W</td>
				</tr>

				<?php for($i = 1; $i <= 9; $i++): ?>
					<tr>
						<td><?php echo $i; ?></td>
						<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->mtrsmen != 0 ? $this->holes[$i-1]->mtrsmen : ''); ?></td>
						<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->parmen != 0 ? $this->holes[$i-1]->parmen : ''); ?></td>
						<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->indexmen1 != 0 ? $this->holes[$i-1]->indexmen1 : ''); ?></td>
						<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->indexmen2 != 0 ? $this->holes[$i-1]->indexmen2 : ''); ?></td>
						<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->mtrswomen != 0 ? $this->holes[$i-1]->mtrswomen : ''); ?></td>
						<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->parwomen != 0 ? $this->holes[$i-1]->parwomen : ''); ?></td>
						<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->indexwomen1 != 0 ? $this->holes[$i-1]->indexwomen1 : ''); ?></td>
						<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->indexwomen2 != 0 ? $this->holes[$i-1]->indexwomen2 : ''); ?></td>
						<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->indexwomen3 != 0 ? $this->holes[$i-1]->indexwomen3 : ''); ?></td>

						<td <?php if ($this->post['usergender'] == 0 || $this->post['usergender'] === null): ?> class="inputcell" <?php else: ?> class="inputcell_readonly" <?php endif; ?>>
							<input type="text" class="celledit" maxlength="5" <?php if ($this->post['usergender'] == 0 || $this->post['usergender'] === null): ?> tabindex="<?php echo $i; ?>" <?php endif; ?> name="scores[player1][<?php echo $i; ?>][score]" id="scores[player1][<?php echo $i; ?>][score]" value="<?php echo ($this->post['scores']['player1'][$i-1]['score'] != 0 ? $this->post['scores']['player1'][$i-1]['score'] : ''); ?>" <?php if ($this->post['usergender'] == 1): ?> readonly="true" <?php endif; ?>/>
						</td>
						<td <?php if ($this->post['usergender'] == 1): ?> class="inputcell" <?php else: ?> class="inputcell_readonly" <?php endif; ?>>
							<input type="text" class="celledit" maxlength="5" <?php if ($this->post['usergender'] == 1): ?> tabindex="<?php echo $i; ?>" <?php endif; ?> name="scores[player2][<?php echo $i; ?>][score]" id="scores[player2][<?php echo $i; ?>][score]" value="<?php echo ($this->post['scores']['player2'][$i-1]['score'] != 0 ? $this->post['scores']['player2'][$i-1]['score'] : ''); ?>" <?php if ($this->post['usergender'] == 0): ?> readonly="true" <?php endif; ?>/>
						</td>
						<?php if ($this->post['scoreid']): ?>
							<input type="hidden" name="scores[player1][<?php echo $i; ?>][id]" id="scores[player1][<?php echo $i; ?>][id]" value="<?php echo $this->post['scores']['player1'][$i-1]['id']; ?>"/></td>
							<input type="hidden" name="scores[player2][<?php echo $i; ?>][id]" id="scores[player2][<?php echo $i; ?>][id]" value="<?php echo $this->post['scores']['player2'][$i-1]['id']; ?>"/></td>
						<?php endif; ?>
					</tr>
				<?php endfor; ?>
			</tbody>
		</table>

		<div style="float:right; padding:0; margin-top: 13px">
		<table cellspacing="0" cellpadding="0" style="text-align:left; width: 230px;padding:0;margin:0">
			<tr>
				<td><?php echo JText::_('Date:'); ?></td>
				<td>
					<?php echo JHTML::_('calendar', $this->post['playdate'], 'playdate', 'playdate', '%d-%m-%Y', array('class'=>'dateinput', 'maxlength'=>'19')); ?>
				</td>
			</tr>
			<?php if ($this->gender_select): ?>
				<tr>
					<td><?php echo JText::_( 'Your gender:' ); ?></td>
					<td>
						<?php echo $this->gender_select; ?>
					</td>
				</tr>
			<?php endif; ?>
		</table>
		</div>
		</div>

		<div class="right">
			<table class="card" cellspacing="0">
				<tbody>
					<tr>
						<td></td>
						<td colspan="4">MEN</td>
						<td colspan="5">WOMEN</td>
						<td colspan="2">PLAYER</td>
					</tr>
					<tr>
						<td>H<br>O<br>L<br>E</td>
						<td>M<br>T<br>R<br>S</td>
						<td>P<br>A<br>R</td>
						<td colspan="2">INDEX</td>
						<td>M<br>T<br>R<br>S</td>
						<td>P<br>A<br>R</td>
						<td colspan="3">INDEX</td>
						<td>M</td>
						<td>W</td>
					</tr>
					<?php for($i = 10; $i <= 18; $i++): ?>
						<tr>
							<td><?php echo $i; ?></td>
							<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->mtrsmen != 0 ? $this->holes[$i-1]->mtrsmen : ''); ?></td>
							<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->parmen != 0 ? $this->holes[$i-1]->parmen : ''); ?></td>
							<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->indexmen1 != 0 ? $this->holes[$i-1]->indexmen1 : ''); ?></td>
							<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->indexmen2 != 0 ? $this->holes[$i-1]->indexmen2 : ''); ?></td>
							<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->mtrswomen != 0 ? $this->holes[$i-1]->mtrswomen : ''); ?></td>
							<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->parwomen != 0 ? $this->holes[$i-1]->parwomen : ''); ?></td>
							<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->indexwomen1 != 0 ? $this->holes[$i-1]->indexwomen1 : ''); ?></td>
							<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->indexwomen2 != 0 ? $this->holes[$i-1]->indexwomen2 : ''); ?></td>
							<td class="inputcell_readonly"><?php echo ($this->holes[$i-1]->indexwomen3 != 0 ? $this->holes[$i-1]->indexwomen3 : ''); ?></td>

							<td <?php if ($this->post['usergender'] == 0 || $this->post['usergender'] === null): ?> class="inputcell" <?php else: ?> class="inputcell_readonly" <?php endif; ?>>
								<input type="text" class="celledit" maxlength="5" <?php if ($this->post['usergender'] == 0 || $this->post['usergender'] === null): ?> tabindex="<?php echo $i; ?>" <?php endif; ?> name="scores[player1][<?php echo $i; ?>][score]" id="scores[player1][<?php echo $i; ?>][score]" value="<?php echo ($this->post['scores']['player1'][$i-1]['score'] != 0 ? $this->post['scores']['player1'][$i-1]['score'] : ''); ?>" <?php if ($this->post['usergender'] == 1): ?> readonly="true" <?php endif; ?>/>
							</td>
							<td <?php if ($this->post['usergender'] == 1): ?> class="inputcell" <?php else: ?> class="inputcell_readonly" <?php endif; ?>>
								<input type="text" class="celledit" maxlength="5" <?php if ($this->post['usergender'] == 1): ?> tabindex="<?php echo $i; ?>" <?php endif; ?> name="scores[player2][<?php echo $i; ?>][score]" id="scores[player2][<?php echo $i; ?>][score]" value="<?php echo ($this->post['scores']['player2'][$i-1]['score'] != 0 ? $this->post['scores']['player2'][$i-1]['score'] : ''); ?>" <?php if ($this->post['usergender'] == 0): ?> readonly="true" <?php endif; ?>/>
							</td>
							<?php if ($this->post['scoreid']): ?>
								<input type="hidden" name="scores[player1][<?php echo $i; ?>][id]" id="scores[player1][<?php echo $i; ?>][id]" value="<?php echo $this->post['scores']['player1'][$i-1]['id']; ?>"/></td>
								<input type="hidden" name="scores[player2][<?php echo $i; ?>][id]" id="scores[player2][<?php echo $i; ?>][id]" value="<?php echo $this->post['scores']['player2'][$i-1]['id']; ?>"/></td>
							<?php endif; ?>
						</tr>
					<?php endfor; ?>
				</tbody>
			</table>

			<div style="height:120px;margin-top: 10px">
				<div style="width:100px;float:left;margin-right:10px">
					<table class="card" cellspacing="0" cellpadding="0">
						<tr>
							<td>IN</td>
							<td class="inputcell_readonly"><input type="text" name="ins" id="ins" class="celledit" maxlength="5" value="<?php echo $this->post['ins']; ?>" readonly /></td>
						</tr>
						<tr>
							<td>OUT</td>
							<td class="inputcell_readonly"><input type="text" name="outs" id="outs" class="celledit" maxlength="5" value="<?php echo $this->post['outs']; ?>" readonly /></td>
						</tr>
						<tr>
							<td>TOTAL</td>
							<td class="inputcell_readonly"><input type="text" name="total" id="total" class="celledit" maxlength="5" value="<?php echo $this->post['total']; ?>" readonly /></td>
						</tr>
					</table>
				</div>

				<div style="width:100px;">
					<table class="card" cellspacing="0">
						<tr>
							<td>A.C.R</td>
							<td class="inputcell_readonly"><?php echo ($this->scorecard->acr != 0 ? $this->scorecard->acr : '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'); ?></td>
						</tr>
						<tr>
							<td>A.W.C.R</td>
							<td class="inputcell_readonly"><?php echo ($this->scorecard->awcr != 0 ? $this->scorecard->awcr : ''); ?></td>
						</tr>
					</table>
				</div>
			</div>
		</div>

		<div style="margin-top: 455px">
		<label><?php echo JText::_('Notes:'); ?></label>

		<textarea name="notes" id="notes" wrap="physical" rows="4"><?php echo $this->post['notes']; ?></textarea>
		</div>
	</div>

	<div style="text-align: right">
		<input type="button" class="button" value="Cancel" onclick="document.location.href='index.php?option=com_scorecard&view=score&Itemid=<?php echo $this->itemid; ?>'"/>
		<input type="button" class="button" value="Save" onclick="scorecardCheckAndSubmit();"/>
	</div>

	<input type="hidden" name="option" value="<?php echo $option; ?>"/>
	<input type="hidden" name="task" value=""/>
	<input type="hidden" name="Itemid" value="<?php echo $this->itemid; ?>"/>
	<?php if($this->post['scoreid']): ?>
		<input type="hidden" name="scoreid" id="scoreid" value="<?php echo $this->post['scoreid']; ?>"/>
	<?php endif; ?>
</form>
